package com.techwells.learningkites.controller;

import java.net.URI;
import java.util.HashMap;

import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.VideoView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.model.SegmentDetailViewModel;
import com.techwells.learningkites.model.VideoViewPlayerViewModel;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.tasktool.TaskToken;

public class VideoViewPlayerActivity extends CommonBaseActivity {

	private com.techwells.taco.widget.FullScreenVideoView videoView;	
	private Dialog dialog;
	private VideoViewPlayerViewModel presentModel;
	private Handler mHandler = new Handler();
	private ProgressDialog progressDialog = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_videoview_player);
		initViews();
	}

	private void initViews() {
//		dialog=ProgressDialog.show(this, null, "视频加载中...");
		videoView = (com.techwells.taco.widget.FullScreenVideoView) findViewById(R.id.videoview);
		
		
//		Uri uri = Uri.parse(getIntent().getStringExtra("resource"));
//		videoView.setVideoURI(uri);
//		videoView.setVideoPath(getIntent().getStringExtra("resource"));
		MediaController mediaCtr = new MediaController(this);
		videoView.setMediaController(mediaCtr);
		videoView.requestFocus();
		videoView.setOnPreparedListener(new OnPreparedListener() {
			
			@Override
			public void onPrepared(MediaPlayer mp) {
//				dialog.dismiss();
				dismissProgressDialog();
				videoView.start();
			}
		});
		
		showProgressDialog();
		
	}
	
	
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		doRequestGetVideoStream();
	}
	
	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		presentModel = (VideoViewPlayerViewModel) baseViewModel;
	}
	
	private void doRequestGetVideoStream() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("url", getIntent().getStringExtra("resource"));
		argMap.put("name", getIntent().getStringExtra("name"));
		doTask(KitesServiceMediator.SERVICE_GET_VIDEO_STREAM,argMap);
		
	}
	
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_VIDEO_STREAM)) {
			String resourceCache = ((VideoViewPlayerViewModel)baseViewModel).resourceCache;
			videoView.setVideoPath(resourceCache);
			
		}
	}
	
	
	private void showProgressDialog() {
	    mHandler.post(new Runnable() {

	        @Override
	        public void run() {
	            if (progressDialog == null) {
	                progressDialog = ProgressDialog.show(VideoViewPlayerActivity.this,
	                        "加载视频", "请稍候...", true, false);
	            }
	        }
	    });
	}

	private void dismissProgressDialog() {
	    mHandler.post(new Runnable() {

	        @Override
	        public void run() {
	            if (progressDialog != null) {
	                progressDialog.dismiss();
	                progressDialog = null;
	            }
	        }
	    });
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		dismissProgressDialog();
	}
}
